﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Function
{
    public class MessageBoxBase
    {
        /// <summary>
        /// 拼接字符串
        /// </summary>
        private string MosaicString;

        public string ToMessStr = "";

        private string pathdepath = Method.RelativePathDepth;

        StringBuilder strWrite = new StringBuilder();

        /// <summary>
        /// 构造方法
        /// </summary>
        public MessageBoxBase()
        {
            StringBuilder strMosaic = new StringBuilder();
            strMosaic.Append(" <script src=\"" + pathdepath + "Js/jquery-1.7.2.min.js\"></script>\r\n");
            strMosaic.Append(" <script src=\"" + pathdepath + "lhgdialog/lhgdialog.js\"></script>\r\n");
            MosaicString = strMosaic.ToString();
        }

        /// <summary>
        /// Alert提示框
        /// </summary>
        /// <param name="messinfo">类容信息</param>
        /// <param name="messurl">跳转地址</param>
        /// <returns></returns>
        public string AlertDialog(string messinfo, string messurl = "")
        {
            ToMessStr = ToMessStr + MosaicString;
            ToMessStr = ToMessStr + "<script language='javascript' type='text/jscript'>\r\n";
            ToMessStr = ToMessStr + "$.dialog.alert('" + messinfo + "',function(){window.location.href = '" + messurl + "';});\r\n";
            ToMessStr = ToMessStr + "</script>\r\n";
            return ToMessStr;
        }

        /// <summary>
        /// 定时关闭消息提示框
        /// 此类提示框会在指定时间后关闭 并执行指定的js代码或者跳转到指定的页面
        /// js API: $.dialog.tips("操作成功", 2, "success.gif", function () { alert('a') })
        /// </summary>
        /// <param name="mtype">消息框类型(成功,失败,警告)</param>
        /// <param name="messinfo">消息内容</param>
        /// <param name="urlOrJavaScirpt">跳转的url或者js脚本内容 取决于参数isscript url为空则不跳转</param>
        /// <param name="parentJump">是否在父窗口容器执行跳转代码</param>
        /// <param name="isScript">消息框动作是跳转指定页面还是执行自定义script</param>
        /// <returns></returns>
        public string TimingDialog(MessType mtype, string messinfo, int timeInSeconds, string urlOrJavaScirpt, bool parentJump = false, bool isScript = false)
        {
            string strtype = String.Empty;
            switch (mtype)
            {
                case MessType.Success:
                    strtype = "success.gif";
                    break;
                case MessType.Error:
                    strtype = "error.gif";
                    break;
                case MessType.Alarm:
                    strtype = "alert.gif";
                    break;
                default:
                    strtype = "";
                    break;
            }
            //处理消息 把引号转义
            messinfo = messinfo.Replace('"', '"').Replace("\r", "").Replace("\n", "");
            if (messinfo.Length > 100)
                messinfo = messinfo.Substring(0, 99) + "...";
            ToMessStr = ToMessStr + MosaicString;
            ToMessStr = ToMessStr + "<script language='javascript' type='text/jscript'>\r\n";
            ToMessStr = ToMessStr + string.Format("$.dialog.tips('{0}',{1},'{2}'", messinfo, timeInSeconds, strtype);
            if (isScript)
                ToMessStr = ToMessStr + ",function () {" + urlOrJavaScirpt + "}";
            else
            {
                if (urlOrJavaScirpt.IsNotEmpty())
                {
                    if (parentJump)
                        ToMessStr = ToMessStr + ",function () {window.parent.location.href = '" + urlOrJavaScirpt + "';}";
                    else
                        ToMessStr = ToMessStr + ",function () {window.location.href = '" + urlOrJavaScirpt + "';}";
                }
            }
            ToMessStr = ToMessStr + ");\r\n</script>\r\n";
            return ToMessStr;
        }

        /// <summary>
        /// 普通消息提示框
        /// </summary>
        /// <param name="mtype">消息框类型(成功,失败,警告)</param>
        /// <param name="messinfo">消息内容</param>
        /// <param name="urlOrJavaScirpt">跳转的url或者js脚本内容 取决于参数isscript url为空则不跳转</param>
        /// <param name="parentJump">是否在父窗口容器执行跳转代码</param>
        /// <param name="isScript">消息框动作是跳转指定页面还是执行自定义script</param>
        /// <returns></returns>
        public string CommonDialog(MessType mtype, string messinfo, string urlOrJavaScirpt = "", bool parentJump = false, bool isScript = false)
        {
            string strtype = String.Empty;
            switch (mtype)
            {
                case MessType.Success:
                    strtype = "icon: 'success.gif',";
                    break;
                case MessType.Error:
                    strtype = "icon: 'error.gif',";
                    break;
                case MessType.Alarm:
                    strtype = "icon: 'alert.gif',";
                    break;
                default:
                    strtype = "";
                    break;
            }
            //处理消息 把引号转义
            messinfo = messinfo.Replace('"', '"').Replace("\r", "").Replace("\n", "");
            if (messinfo.Length > 100)
                messinfo = messinfo.Substring(0, 99) + "...";
            ToMessStr = ToMessStr + MosaicString;
            ToMessStr = ToMessStr + "<script language='javascript' type='text/jscript'>\r\n";
            ToMessStr = ToMessStr + "$.dialog({";
            ToMessStr = ToMessStr + "lock: true,cancel:false,";
            ToMessStr = ToMessStr + strtype;
            ToMessStr = ToMessStr + "content:\"" + messinfo + "\",";
            if (isScript)
                ToMessStr = ToMessStr + "ok: function () {" + urlOrJavaScirpt + "}";
            else
            {
                if (urlOrJavaScirpt.IsEmpty())
                    ToMessStr = ToMessStr + "ok: true";
                else
                {
                    if (parentJump)
                        ToMessStr = ToMessStr + "ok: function () {window.parent.location.href = '" + urlOrJavaScirpt + "';}";
                    else
                        ToMessStr = ToMessStr + "ok: function () {window.location.href = '" + urlOrJavaScirpt + "';}";
                }
            }
            ToMessStr = ToMessStr + "});\r\n</script>\r\n";
            return ToMessStr;
        }

        /// <summary>
        /// 消息类型
        /// </summary>
        public enum MessType
        {
            /// <summary>
            /// 成功
            /// </summary>
            Success,
            /// <summary>
            /// 失败
            /// </summary>
            Error,
            /// <summary>
            /// 警告
            /// </summary>
            Alarm
        }
    }
}
